package core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Marcin
 */
public class Authorization {
    private JFrame parent;
    public Authorization(JFrame parent)
    {
        this.parent = parent;
    }
    public int logIn(String username,String password)
    { 
        try {
            Statement getpassword = SqlConnection.getInstance().getSqlConnection().createStatement();
            getpassword.execute("SELECT password,id_uprawnienia FROM user WHERE login='"+username+"';");
            ResultSet rs = getpassword.getResultSet();
            
            if((rs.next() && rs.getString(1) != null) && rs.getString(1).equals(password)){
                return Integer.parseInt(rs.getString(2));
            }
            else {
                ErrorLogger.getInstance().log("Niepoprawny login lub hasło", true, parent);
                return -1;
            }
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
        }
        return -1;
    }
}
